package com.yunshang.shoppingmall.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunshang.shoppingmall.common.MessageConstants;
import com.yunshang.shoppingmall.common.Response;
import com.yunshang.shoppingmall.common.layuiJson;
import com.yunshang.shoppingmall.entity.Address;
import com.yunshang.shoppingmall.entity.Area;
import com.yunshang.shoppingmall.service.AddressService;
import com.yunshang.shoppingmall.service.AreaService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.swing.plaf.LayerUI;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AddressController {

    @Autowired
    AddressService addressService;
    @Autowired
    AreaService areaService;
    //地址列表数据
    @RequestMapping("/getAddressList")
    @ResponseBody
    public String getAddressList(int page,int limit)
    {
        PageHelper.startPage(page,limit);
        List<Address> addressList=addressService.selectAddressAll();
        PageInfo page1 = new PageInfo(addressList);
        return layuiJson.layuiJsonList(addressList,(int)page1.getTotal());
    }
//地址列表
    @RequestMapping("/addressList")
    public String addressList()
    {
        return "address/addressList";
    }

    @ResponseBody
    @RequestMapping("/addAddress")
    //增加用户地址
    public Response addAddress(Integer provinceId,Integer cityId,Integer areaId,String detailAdder,String name,String phone)
    {
//        Address address=new Address();
//        address.setUserId(1);
//        address.setProvinceId(1);
//        address.setCityId(72);
//        address.setAreaId(2799);
//        address.setDetailAddr("详细地址");
//

        for (int i = 0; i <150; i++) {
            Address address=new Address();
        address.setUserId(1);
        address.setProvinceId(1);
        address.setCityId(72);
        address.setAreaId(2799);
            address.setDetailAddr("详细地址"+i);
            address.setName("张三"+i);
            address.setPhone("18984051111");
            int res=addressService.insert(address);
        }
        //address.setName("张三");
//        address.setPhone("18984051111");
//        int res=addressService.insert(address);
        return new Response(MessageConstants.MSG_SUCCESS,1,"");
    }

    @ResponseBody
    @RequestMapping("/editAddress")
    //修改用户地址
    public Response editAddress(Integer provinceId,Integer cityId,Integer areaId,String detailAdder,String name,String phone,Integer addressId)
    {
        String errorInfo="";
        Address address=addressService.selectByPrimaryKey(addressId);
        if(provinceId!=0){address.setProvinceId(1);}
        if(cityId!=0){address.setCityId(1);}
        if(areaId!=0){address.setAreaId(1);}
        address.setUserId(1);
        if(detailAdder==""){errorInfo="详细地址不能为空!";}
        if(name==""){errorInfo="姓名不能为空!";}
        if(phone==""){errorInfo="手机号不能为空!";}
        if(errorInfo!="")
        {
            return new Response(MessageConstants.MSG_SUCCESS,-1,errorInfo);
        }
        address.setDetailAddr("详细地址");
        address.setName("张三");
        address.setPhone("18984051111");
        int res=addressService.updateByPrimaryKey(address);
        return new Response(MessageConstants.MSG_SUCCESS,res,"");
    }

    @ResponseBody
    @RequestMapping("/deleteAddress")
    //删除用户地址
    public Response deleteAddress(Integer addressId)
    {
        int res=addressService.deleteByPrimaryKey(addressId);
        return new Response(MessageConstants.MSG_SUCCESS,res,"");
    }
    @ResponseBody
    @RequestMapping("/A1")
    public void getAddress(HttpServletResponse response1) throws IOException {
        List<Area> areas=areaService.selectListAll();
        String areaString= JSON.toJSONString(areas);
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        response.setContentType("application/json;charset=utf-8");

        ServletOutputStream out;
        try {
            out = response.getOutputStream();
 byte data[] = areaString.getBytes();
 out.write(data);
 out.close();
 } catch (IOException e) {
 e.printStackTrace();
  }


    }
}
